﻿package com.dykj.platform.iadapter.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;

import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.iadapter.common.Constants;

public class FileUtils {

	public FileUtils() {
	}

	public static String switchToNetPath(String path) {
		String res = path.replaceAll("\\\\", "/");
		return res;
	}

	/**
	 * 从文本文件中读取内容
	 * 
	 * @param path
	 *            文件路径
	 * @return 从文本文件中读取的内容
	 * @throws ApplicationException
	 * @throws IOException
	 */
	public static String[] readfile(String path) throws IOException {
		BufferedReader bufread;
		String read;
		List listStr = new ArrayList();

		File file = new File(path);
		FileReader fileread = new FileReader(file);
		bufread = new BufferedReader(fileread);
		while ((read = bufread.readLine()) != null) {
			listStr.add(read);
		}

		String[] readStr = ActionUtils.list2Array(listStr);
		return readStr;
	}

	// 向文本文件中写入内容
	public void writefile(String path, String content, boolean append) {
		try {
			String filecontent;
			File writefile;
			boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
			filecontent = content; // 需要写入的内容
			writefile = new File(path);
			if (writefile.exists() == false) // 如果文本文件不存在则创建它
			{
				writefile.createNewFile();
				writefile = new File(path); // 重新实例化
			}
			FileWriter filewriter = new FileWriter(writefile, addStr);
			// BufferedWriter bufwriter = new BufferedWriter(filewriter);
			filewriter.write(filecontent);
			filewriter.flush();
		} catch (Exception d) {
			System.out.println(d.getMessage());
		}
	}

	/**
	 * 读取文件返回二进制
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static byte[] readFileByte(String path) throws IOException {
		File file = new File(path);
		if (!file.exists() || file.isDirectory())
			throw new FileNotFoundException();
		FileInputStream fis = new FileInputStream(file);
		byte[] buf = new byte[fis.available()];
		fis.read(buf);
		fis.close();
		return buf;
	}

	/**
	 * 读取文件返回字符串
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readFileString(String path) throws IOException {
		File file = new File(path);
		if (!file.exists() || file.isDirectory())
			throw new FileNotFoundException();
		FileInputStream fis = new FileInputStream(file);
		byte[] buf = new byte[fis.available()];
		fis.read(buf);
		String sss = new String(Base64.encodeBase64(buf));
		fis.close();
		return sss;
	}

	/**
	 * 写二进制文件
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static void writeByteFile(byte[] files, String path)
			throws IOException {
		File file = new File(path);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(Base64.decodeBase64(files));
		fos.close();
	}

	/**
	 * 以文件流的方式复制文件
	 * 
	 * @param src
	 *            文件源目录
	 * @param dest
	 *            文件目的目录
	 * @throws IOException
	 */
	public static void copyFile(String src, String dest) throws IOException {
		FileInputStream in = new FileInputStream(src);
		File file = new File(dest);
		if (!file.exists())
			file.createNewFile();
		FileOutputStream out = new FileOutputStream(file);
		int c;
		byte buffer[] = new byte[1024];
		while ((c = in.read(buffer)) != -1) {
			for (int i = 0; i < c; i++)
				out.write(buffer[i]);
		}
		in.close();
		out.close();
	}

	/**
	 * 传入原先的路径，判断在此路径下是否有相应的文件存在，返回修改后的路径。
	 * 如果使用tomcat服务器，可能需要修改一下；
	 * 
	 * @param path
	 *            原先的路径
	 * @param nopicPath  假如图片不存在，候补图片的路径
	 * @return path 在weblogic中修改后的路径
	 * @author zhouwendong
	 */
	public static String getPicPathInWeblogic(String path, String nopicPath) {
		if (null != path) {
			String oldstr = Constants.REALFILE_PATH+ path;
			File oldfile = new File(oldstr);
			// 判断此文件是否存在
			if (!oldfile.exists()) {
				path = nopicPath;
			}else{
				path = oldstr;
			}
		} else {
			path = Constants.REALFILE_PATH+nopicPath;
		}
		return path;
	}
}
